package com.tea.source.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Component;

import com.tea.source.model.Sales;



@Component("salesDao")
public class SalesDao extends BaseDao{
	
	public List<Sales> load(){
		List<Sales> sales = (List<Sales>)hibernateTemplate.find("from Sales s");
		return sales;
	}
	public boolean isSalesExit(String xsserialnum){
		List<Sales> s = (List<Sales>)hibernateTemplate.find("from Sales s where s.xsserialnum='"+xsserialnum+"'");
		if(s!=null && s.size()>0)
			return true;
		else
			return false;
	}

	public Sales loadById(int id) {
		List<Sales> s = (List<Sales>)hibernateTemplate.find("from Sales s where s.id='"+id+"'");
		
		return s.get(0);
	}

	public Sales loadByXsid(String xsserialnum) {
		List<Sales> s = (List<Sales>)hibernateTemplate.find("from Sales s where s.xsserialnum='"+xsserialnum+"'");
		
		return s.get(0);
	}
	

	public void save(Sales sales) {
		
		hibernateTemplate.save(sales);
	}
	

	public void delete(Sales sales) {
		deleteById(sales.getId());
		
	}

	public void deleteById(final int id) {
		hibernateTemplate.execute(new HibernateCallback()
		{
		public Object doInHibernate(Session session)
		throws HibernateException
		{
		session.createQuery("delete from Sales s where s.id=:id").setParameter("id", id).executeUpdate();
		return null;
		}
		});
		
	}
}
